<?php

namespace app\zmt\controller;

use cmf\controller\AdminBaseController;
use app\zmt\model\NavModel;
use app\zmt\validate\NavValidate;

class NavController extends AdminBaseController
{
    public function index()
    {
        // 获取搜索关键字
        $keyword = input('get.keyword', '');
        $where = [];
        if ($keyword) {
            $where[] = ['title', 'like', '%' . $keyword . '%'];
        }

        // 查询所有导航并传递给视图
        $navList = NavModel::where($where)
            ->order('list_order', 'desc')
            ->paginate(10); // 每页显示10条记录

        $page = $navList->render();
        $this->assign('navList', $navList);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch('add');
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            // 验证数据
            $validate = new NavValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $nav = new NavModel();
            $nav->title = $data['title'];
            $nav->url = $data['url'];
            $nav->image = $data['image'];
            $nav->switch = isset($data['switch']) ? $data['switch'] : 1;
            $nav->create_time = date('Y-m-d H:i:s');

            if ($nav->save()) {
                $this->success('添加成功', url('Nav/index'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    public function changeStatus()
    {
        $id = input('param.id', 0, 'intval'); // 获取传入的新闻 ID
        $news = NavModel::find($id);

        if ($news) {
            // 切换状态
            $news->switch = $news->switch == 1 ? 0 : 1;

            if ($news->save()) {
                $this->success('状态切换成功', 'Nav/index'); // 成功后重定向到新闻列表页
            } else {
                $this->error('状态切换失败', 'Nav/index'); // 失败后也重定向到新闻列表页
            }
        } else {
            $this->error('新闻不存在', 'Nav/index'); // 新闻不存在时重定向到新闻列表页
        }
    }

    public function edit($id)
    {
        $nav = NavModel::find($id);
        if (!$nav) {
            $this->error('导航不存在');
        }

        $this->assign('nav', $nav);
        return $this->fetch('edit');
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();

            // 验证数据
            $validate = new NavValidate();
            if (!$validate->check($data)) {
                $this->error($validate->getError());
            }

            $nav = NavModel::find($data['id']);
            if (!$nav) {
                $this->error('导航不存在');
            }

            $nav->title = $data['title'];
            $nav->url = $data['url'];
            $nav->image = $data['image'];
            $nav->switch = isset($data['switch']) ? $data['switch'] : 1;
            $nav->update_time = date('Y-m-d H:i:s');

            if ($nav->save()) {
                $this->success('编辑成功', url('Nav/index'));
            } else {
                $this->error('编辑失败');
            }
        }
    }

    public function delete($id)
    {
        $nav = NavModel::find($id);
        if (!$nav) {
            $this->error('导航不存在');
        }

        if (NavModel::destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

    public function order()
    {
        parent::listOrders(new NavModel());
        $this->success('排序成功');
    }

}
